package com.sinven.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinven.pojo.Article;
import com.sinven.pojo.User;
import com.sinven.service.ArticleService;
import com.sinven.service.UserService;
import com.sinven.util.PageBean;



/**
 * 留言
 * @author admin
 *
 */
@Controller
public class HTArticleController {
	@Autowired
	private ArticleService article;
	
	@Autowired
	private UserService user;
	
	
	@RequestMapping({"HT"})
	public String login(HttpSession session,Model model) {
		User user1 = (User)session.getAttribute("user");
		try {
			User login = user.login(user1);
			model.addAttribute("login", login);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pages/HT";
		
	}
	//文章管理
	@RequestMapping({"theArticleManagement"})
	public String article(Model model ,@RequestParam(value="pageNum",defaultValue="1",required=true) int pageNum,@RequestParam(value="pageSize",defaultValue="5",required=true) int pageSize
			,@RequestParam(value="pageNum2",defaultValue="1",required=true) int pageNum2,@RequestParam(value="pageSize2",defaultValue="5",required=true) int pageSize2
			,@RequestParam(value="pageNum3",defaultValue="1",required=true) int pageNum3,@RequestParam(value="pageSize3",defaultValue="5",required=true) int pageSize3
			) {
		try {
			PageBean<Article> ThePublished = article.SelectArticleBypostTimeAndstate(1, pageNum, pageSize);//已发布文章
			PageBean<Article> ToAdit = article.SelectArticleBypostTimeAndstate(4, pageNum2, pageSize2);//审核中
			PageBean<Article> Deleted = article.SelectArticleBypostTimeAndstate(5, pageNum3, pageSize3);
			
			model.addAttribute("ThePublished", ThePublished);
			model.addAttribute("ToAdit", ToAdit);
			model.addAttribute("Deleted", Deleted);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pages/theArticleManagement";
	}
	
		//用户管理
		@RequestMapping({"theUserManagement"})
		public String user(Model model ,@RequestParam(value="pageNum",defaultValue="1",required=true) int pageNum,@RequestParam(value="pageSize",defaultValue="5",required=true) int pageSize) {
			try {
				PageBean<User> selectAll = user.SelectAll(pageNum, pageSize);
				System.out.println(selectAll.getListUser().size());
				model.addAttribute("selectAll", selectAll);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "pages/theUserManagement";
		}
				//权限管理
				@RequestMapping({"theRightsManagement"})
				public String right(Model model ,@RequestParam(value="pageNum",defaultValue="1",required=true) int pageNum,@RequestParam(value="pageSize",defaultValue="5",required=true) int pageSize) {
					try {
						PageBean<User> selectAllByType = user.SelectAllByType("普通用户", pageNum, pageSize);
						model.addAttribute("selectAllByType", selectAllByType);
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return "pages/theRightsManagement";
				}
				
				
			
				//修改状态
				@RequestMapping({"xgzt"})
				public String xgzt(@RequestParam(value="u_id")int u_id,@RequestParam(value="state")int state) {
					try {
						System.out.println(state);
						System.out.println(u_id);
						int a = 1;
						if(state==1) {
							a = 0;
							
						}else {
							a = 1;
						}
						System.out.println(a+"*****************^^");
						boolean updateUserState = user.UpdateUserState(u_id, a);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return "forward:theRightsManagement";
				}
				
	
		//查看文章
		@RequestMapping({"ckArticle"})
		public String cxArticle(Model model, @RequestParam(value="a_id")int a_id) {
			try {
				Article selectArticleContent = article.SelectArticleContent(a_id);
				User selectUserById = user.SelectUserById(selectArticleContent.getA_authorId().getU_id());
				model.addAttribute("selectArticleById", selectArticleContent);
				model.addAttribute("selectUserById", selectUserById);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "pages/ckArticle";
		}
		
		//查看用户
				@RequestMapping({"ckUser"})
				public String cxUser(Model model, @RequestParam(value="u_id")int u_id) {
					try {
						 User userid = user.SelectUserById(u_id);
						 model.addAttribute("selectUserById",userid);
					} catch (Exception e) {
						
					}
					return "pages/ckUser";
				}
		
	//操作
	@RequestMapping({"htcz"})
	public String state(@RequestParam(value="a_id")int a_id,@RequestParam(value="state")int state) {
		try {
			boolean updateState = article.UpdateState(a_id, state);
			if(updateState) {
				
				return "forward:theArticleManagement";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "forward:theArticleManagement";
		
		
		
	}
	
	
}
